using Dapr.Client;
using Furion.DynamicApiController;
using Kevin.Dapr.UserService;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;

namespace Kevin.Dapr.Api.Controllers
{
    public partial class DaprGrpcController : IDynamicApiController
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="userName"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        public async Task<dynamic> Login(string userName, string password)
        {
            var data = await _daprClient.InvokeMethodGrpcAsync<LoginInput, LoginOutput>("daprgrpcservice", "login", new LoginInput { UserName = userName, Password = password });
            return data;
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="userName"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        public async Task Savestorage(string userName, string password)
        {
            await _daprClient.InvokeMethodGrpcAsync("daprgrpcservice", "savestorage");
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="userName"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        public async Task<LoginOutput> Getstorage(string userName, string password)
        {
            var data = await _daprClient.InvokeMethodGrpcAsync<LoginOutput>("daprgrpcservice", "getstorage");
            return data;
        }

        [HttpGet]
        public async Task SaveToRedis(string name)
        {
            await _daprClient.SaveStateAsync("statestore", "login:user:18888", JsonConvert.SerializeObject(new
            {
                Id = 18888,
                Name = name,
                Age = 18,
                UserName = "admin",
                Address = "天府五街"
            }));
        }

        [HttpGet]
        public async Task<string> GetFromRedis()
        {
            var data = await _daprClient.GetStateAsync<string>("statestore", "login:user:18888");
            return data;
        }
    }

    public partial class DaprGrpcController
    {
        private readonly DaprClient _daprClient;

        public DaprGrpcController(DaprClient daprClient)
        {
            _daprClient = daprClient;
        }
    }
}




